package com.example.demo.repository;

import com.example.demo.entity.EnumEducation;
import com.example.demo.entity.Person;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.query.Procedure;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by pengwan on 2017/7/29.
 */
@Repository
public interface PersonRepository extends JpaRepository<Person, String> {
    // jpa 自带查询
    Person findFirstByFirstNameAndLastName(String firstName, String lastName);
    List<Person> findByFirstNameLikeOrLastNameLike(String firstName, String lastName);
    List<Person> findByFirstNameStartingWith(String firstName);
    List<Person> findByLastNameEndingWith(String lastName);
    List<Person> findByLastNameContaining(String fistName);
    List<Person> findByBirthdayBetween(Date startDate, Date endDate);
    List<Person> findByWeightLessThan(double weight);
    List<Person> findByWeightGreaterThan(double weight);
    List<Person> findByBirthdayBefore(Date date);
    List<Person> findByBirthdayAfter(Date date);
    List<Person> findByActiveTrueOrderByBirthdayDesc();
    List<Person> findByEducationIn(Collection<EnumEducation> educations);

    // 用于service 的自定义查询
    Page<Person> findAll(Specification<Person> personSpecification, Pageable pageable);

    // hql 查询
    @Query("select p from Person p where p.address.city like :city")
    List<Person> hqlFindByCity(@Param("city") String city);

    // sql 查询
    @Query(value = "select * from person p where p.first_name||p.last_name like '%'||:name||'%'", nativeQuery = true)
    List<Person> sqlFindByName(@Param("name") String name);
}

